package com.tusuo.stds.ui.scene;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.RelativeLayout;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.tusuo.stds.Constants;
import com.tusuo.stds.MyApp;
import com.tusuo.stds.R;
import com.tusuo.stds.di.DaggerScope;
import com.tusuo.stds.di.Injector;
import com.tusuo.stds.rest.ModuleList;
import com.tusuo.stds.rest.SceneList;
import com.tusuo.stds.ui.BaseToolbarActivity;
import com.tusuo.stds.ui.ToolbarOwner;
import com.tusuo.stds.ui.integrity.IntegrityFragment_;
import com.tusuo.stds.ui.main.TabEntity;
import com.tusuo.stds.ui.module.BonusLevelListFragment;
import com.tusuo.stds.ui.module.MainLevelListFragment_;
import com.tusuo.stds.ui.rank.RankActivity;
import com.tusuo.stds.ui.rank.RankActivity_;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.parceler.Parcels;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;
import myandroid.library.util.ApplicationHelper;
import rx.functions.Action0;
import timber.log.Timber;

@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(SceneActivity.class)
@EActivity(R.layout.activity_scene)
public class SceneActivity extends BaseToolbarActivity<SceneView, ScenePresenter>
                                implements SceneView {

    public static final String FROM_SELF = "from_self";

    @Inject
    SharedPreferences mSharedPref;

    @ViewById(R.id.scene_tab)
    CommonTabLayout mTabLayout;

    @ViewById(R.id.scene_layout_guide)
    RelativeLayout layoutGuide;

    private SceneList.Scene mScene;
    private String[] mTitles;
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private ArrayList<Fragment> mFragmentList = new ArrayList<>();

    @AfterInject
    void init() {
        Injector.initSceneActivityComponent().inject(this);

        mScene = Parcels.unwrap(getIntent().getParcelableExtra(SceneList.Scene.EXTRA_NAME));
    }

    @AfterViews
    void initData() {
        setupUI(this);

        String toolbarTitle = mScene.Title;

        List<ToolbarOwner.MenuAction> menuActionList = new ArrayList<>();

        ToolbarOwner.MenuAction menuAction = new ToolbarOwner.MenuAction("排行榜", new Action0() {
            @Override
            public void call() {
                Intent intent = new Intent();
                intent.setClass(SceneActivity.this, RankActivity_.class);
                intent.putExtra("type", RankActivity.RANK_TYPE_SCENE);
                intent.putExtra("id", mScene.Id);
                startActivity(intent);
            }
        }, 0);

        menuActionList.add(menuAction);

        ToolbarOwner.Config config = new ToolbarOwner.Config(toolbarTitle, true, menuActionList);
        mToolbarOwner.setConfig(config);

        ApplicationHelper.getInstance().addActivity(this);

        if (mSharedPref.getBoolean("firstScene", true)) {
            SharedPreferences.Editor editor = mSharedPref.edit();
            editor.putBoolean("firstScene", false);
            editor.apply();
            layoutGuide.setVisibility(View.VISIBLE);
        } else {
            layoutGuide.setVisibility(View.GONE);
        }

        presenter.loadData();

    }

    @NonNull
    @Override
    public ScenePresenter createPresenter() {
        return new ScenePresenter(this, mScene.Id);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mTabLayout != null && mTabLayout.getTabCount() > 1) {
            presenter.loadData();
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
//        super.onSaveInstanceState(outState, outPersistentState);
    }

    @Override
    public void setModuleList(ModuleList list) {
        mTabEntities = new ArrayList<>();
        mFragmentList = new ArrayList<>();
        mFragmentList.add(SceneFragment_.builder().mScene(mScene).Data(list.Data).build());

        if (mScene.CompleteType == 1) {
            mTitles = new String[]{"模块列表"};
            mTabLayout.setVisibility(View.GONE);
        }else {
            mFragmentList.add(IntegrityFragment_.builder().mDataId(mScene.Id).mType(Constants.CODE_COMPLETE_TYPE_SCENE).build());
            mTitles = new String[]{"模块列表", "知识完整度"};
            mTabLayout.setVisibility(View.VISIBLE);
        }

        for (int i = 0; i < mTitles.length ; i ++) {
            mTabEntities.add(new TabEntity(mTitles[i], 0, 0));
        }

        if (mTabLayout.getTabCount() > 1) {
            mTabLayout.notifyDataSetChanged();
        }else {
            mTabLayout.setTabData(mTabEntities, this, R.id.scene_content, mFragmentList);
        }

//        SceneFragment_ sceneFragment = (SceneFragment_) mFragmentList.get(0);
//        sceneFragment.Data = list.Data;
//        sceneFragment.initData();
//
//        if (mFragmentList.size() > 1) {
//            IntegrityFragment_ integrityFragment = (IntegrityFragment_) mFragmentList.get(1);
//            integrityFragment.initData();
//        }
    }

    @Click(R.id.main_btn_close_guide)
    void hideGuideLayout() {
        layoutGuide.setVisibility(View.GONE);
    }
}
